module BenchmarkForRails
  class << self
    def report_with_juggernaut(request)
      request_action = "#{request.method.to_s.upcase} #{request.path}"
      request_time   = results.delete(:request)
      data = [{:request_action => request_action, 
               :key => 'total', 
               :value => "#{'%.4f' % request_time}", 
               :controller => request.parameters[:controller], 
               :action => request.parameters[:action],
               :request_method => request.env['REQUEST_METHOD']}
              ]

      results.to_a.each do |(name, seconds)|
        data << {:key => name, :value => "#{'%.4f' % seconds}"}
      end
      logger.info "*** Juggernaut sending data"
      Juggernaut.send_to_channels(data, ['log_channel'])
      results.clear      
    end
    alias_method :report_without_juggernaut, :report
    alias_method :report, :report_with_juggernaut
  end
end